using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO.WIX;
using FiltronASP.Models.DAO.Extensions;
using FiltronASP.Models.DAO.Interfaces.WIX;
using FiltronASP.Models.DAO.Filtron;
using FiltronASP.Models.DAO.Interfaces.Filtron;


namespace FiltronASP.Helpers.AjaxModel
{
    /// <summary>
    /// Klasa pomocnicza do generowania stringa z lista modeli.
    /// </summary>
    public static class AutoFillReplacementsHelper
    {

        /// <summary>
        /// Metoda generująca JSonowy
        /// </summary>
        /// <param name="givenString">Część stringa do wygenerowania.</param>
        /// <returns>JSON'owy obiekt z:
        /// query - wpisana część stringa,
        /// suggestions - wygenerowane podpowiedzi
        /// </returns>
        public static string FillInputByGivenString(string givenString)
        {
            if (givenString == "") {
                return String.Empty;
            }
            string resultToReturn = "";
            // JSON'owa część do zwrócenia
            string queryToReturn = "";
            string suggestionsToReturn = "";
            queryToReturn = givenString;

            IWixFilterDAO wixFilterDAO = new WixFilterDAO();
            foreach (WixFilter filter in wixFilterDAO.FindByFilterNumber(givenString)) {
                if (filter != null)
                {
                    suggestionsToReturn += "'" + filter.filter_number_trim + "', ";
                }
                else 
                {
                    return null;
                    //suggestionsToReturn += "''";
                }
            } //feach

            resultToReturn = "{ query:'"+queryToReturn+"',suggestions:["+suggestionsToReturn+"]}";

            return resultToReturn;
        }

        public static string FillInputByGivenStringReplacement(string givenString)
        {
            if (givenString == String.Empty)
            {
                return String.Empty;
            }
            string resultToReturn = String.Empty;
            // JSON'owa część do zwrócenia
            string queryToReturn = String.Empty;
            string suggestionsToReturn = String.Empty;
            queryToReturn = givenString;
            List<String> filtersAlreadyAdded = new List<String>();

            IFiltronFilterDAO filtronFilterDAO = new FiltronFilterDAO();
            IQueryable<FiltronFilter> Filtronfilters = filtronFilterDAO.FindByWixFilterNumberContains(givenString);
            if (Filtronfilters != null)
            {
                foreach (FiltronFilter single in Filtronfilters) {
                    if (!filtersAlreadyAdded.Contains(Helpers.CatalogueHelpers.FiltronHelper.GetFiltronFilterWixNumber(single.filter_wix_number, single.filter_wix_number_hd)))
                    {
                        suggestionsToReturn += "'" + Helpers.CatalogueHelpers.FiltronHelper.GetFiltronFilterWixNumber(single.filter_wix_number, single.filter_wix_number_hd) + "', ";
                        filtersAlreadyAdded.Add(Helpers.CatalogueHelpers.FiltronHelper.GetFiltronFilterWixNumber(single.filter_wix_number, single.filter_wix_number_hd));
                    }
                }
            }

            IWixFilterDAO wixFilterDAO = new WixFilterDAO();
            IQueryable<WixFilter> Wixfilters = wixFilterDAO.FindByFilterNumber(givenString);
            if (Wixfilters != null)
            {
                foreach (WixFilter single in Wixfilters)
                {
                    if (!filtersAlreadyAdded.Contains(single.filter_number_trim))
                    {
                        suggestionsToReturn += "'" + single.filter_number_trim + "', ";
                        filtersAlreadyAdded.Add(single.filter_number_trim);
                    }
                }
            }

            IWixInterchangeDAO wixInterchangeDAO = new WixInterchangeDAO();
            IQueryable<WixInterchange> interchangeCollectionEquals = wixInterchangeDAO.FindByForeignFilterNameEquals(givenString);
            if (interchangeCollectionEquals != null)
            {
                foreach (WixInterchange filter in interchangeCollectionEquals)
                {
                    if (filter != null)
                    {
                        if (!filtersAlreadyAdded.Contains(filter.foreign_filter_trim))
                        {
                            suggestionsToReturn += "'" + filter.foreign_filter_trim + "', ";
                            filtersAlreadyAdded.Add(filter.foreign_filter_trim);
                        }
                    }
                } //feach
            }

            IFiltronInterchangeDAO filtronInterchangeDAO = new FiltronInterchangeDAO();
            IQueryable<FiltronInterchange> interchangesCollectionFiltronEquals = filtronInterchangeDAO.FindByForeignFilterNameEquals(givenString);
            if (interchangesCollectionFiltronEquals != null)
            {
                foreach (FiltronInterchange filter in interchangesCollectionFiltronEquals)
                {
                    if (filter != null)
                    {
                        if (!filtersAlreadyAdded.Contains(filter.foreign_filter_trim))
                        {
                            suggestionsToReturn += "'" + filter.foreign_filter_trim + "', ";
                            filtersAlreadyAdded.Add(filter.foreign_filter_trim);
                        }
                    }
                }
            }

            IQueryable<WixInterchange> interchangeCollection = wixInterchangeDAO.FindByForeignFilterName(givenString);
            if (interchangeCollection != null) {
                foreach (WixInterchange filter in interchangeCollection)
                {
                    if (filter != null)
                    {
                        if (!filtersAlreadyAdded.Contains(filter.foreign_filter_trim))
                        {
                            suggestionsToReturn += "'" + filter.foreign_filter_trim + "', ";
                            filtersAlreadyAdded.Add(filter.foreign_filter_trim);
                        }
                    }
                } //feach
            }

            //Filtron
            IQueryable<FiltronInterchange> interchangesCollectionFiltron = filtronInterchangeDAO.FindByForeignFilterName(givenString);
            if (interchangesCollectionFiltron != null) {
                foreach (FiltronInterchange filter in interchangesCollectionFiltron)
                {
                    if (filter != null)
                    {
                        if (!filtersAlreadyAdded.Contains(filter.foreign_filter_trim))
                        {
                            suggestionsToReturn += "'" + filter.foreign_filter_trim + "', ";
                            filtersAlreadyAdded.Add(filter.foreign_filter_trim);
                        }
                    }
                }
            }

            resultToReturn = "{ query:'" + queryToReturn + "',suggestions:[" + suggestionsToReturn + "]}";

            return resultToReturn;
        }

        
    }
}
